import { BinaryHeap, TCompareCallback } from '../utils/binary-heap';

/**
 * 短语
 */
export class PhraseInfo {
  /**
   * 输入的拼音
   */
  readonly inputYinJieList: string[];
  /**
   * 纠错后的拼音
   */
  readonly yinJieList: string[];
  /**
   * 短语值
   */
  readonly phrase: string;

  constructor(inputYinJieList: string[], yinJieList: string[], phrase: string) {
    this.inputYinJieList = inputYinJieList;
    this.yinJieList = yinJieList;
    this.phrase = phrase;
  }
}

export class Path {
  readonly phraseInfoList: PhraseInfo[];
  readonly score: number;

  constructor(phraseInfoList: PhraseInfo[], score: number) {
    this.phraseInfoList = phraseInfoList;
    this.score = score;
  }
}

const greatThan: TCompareCallback<Path> = (a, b) => a.score - b.score;

/**
 * 优先集
 */
export class PrioritySet {
  private paths = new BinaryHeap<Path>(greatThan);
  private capacity?: number;

  constructor(maxSize?: number) {
    this.capacity = maxSize;
  }

  /**
   * 向优先集中添加一个路径
   */
  put(item: Path): void {
    this.paths.put(item);
    if (this.capacity && this.paths.getSize() > this.capacity) {
      this.paths.pop();
    }
  }

  /**
   * 获取所有路径（无序的）
   */
  getPaths = (): readonly Path[] => {
    return this.paths.toList();
  }

  /**
   * 获取所有路径（按照从大到小顺序排列）
   */
  getSortedPaths = (): Path[] => {
    return this.paths.toList().slice().sort((a, b) => b.score - a.score);
  }
}